layui.use(['layer', 'table', 'form'], function () {
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;
    var $ = layui.$;

    //表单重新渲染
    form.render();

    //表格渲染
    var tableObj = table.render({
        id: 'list_table'
        , elem: '#list_table'
        , url: 'sys/user/list'
        , align: "center"
        , cols: [[ //表头
            {type: 'checkbox'}
            , {type: 'numbers', title: '序号', width: 50}
            , {field: 'userName', title: '用户名', width: 140, align: 'center'}
            , {field: 'userNickname', title: '昵称', width: 120, align: 'center'}
            , {field: 'userMobile', title: '手机号', width: 120, align: 'center'}
            , {field: 'orgName', title: '所属机构', width: 120, align: 'center'}
            , {field: 'userEmail', title: '邮箱', width: 150, align: 'center'}
            , {
                field: 'userSex', title: '性别', width: 70, align: 'center', templet: function (d) {
                    return d.userSex == 1 ? '男' : '女';
                }
            }
            , {
                field: 'userState', title: '状态', width: 70, align: 'center', templet: function (d) {
                    return d.userState == 1 ? '<span class="font-success">启用</span>' : d.userState == 0 ? '<span class="font-danger">禁用</span>' : '<span class="font-disabled">锁定</span>';
                }
            }
            , {field: 'createTime', title: '添加时间', width: 170, align: 'center'}
            , {
                field: 'lock', title: '操作', width: 170, templet: function (d) {
                    return d.isSystem == 1 ?
                    '<button class="layui-btn layui-btn-sm layui-btn-disabled" lay-event="edit" disabled>修改</button>' + '<button class="layui-btn layui-btn-sm layui-btn-disabled" lay-event="settingRoles" disabled>设置角色</button>' :
                    '<button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="edit">修改</button>' + '<button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="settingRoles">设置角色</button>';
                }, unresize: true, align: 'center'
            }
        ]]
    });

    //监听工具条
    table.on('tool(mainList)', function (obj) {
        var data = obj.data
            , layEvent = obj.event;
        //修改
        if (layEvent === 'edit') {
            showDialog({
                title: '修改'
                , template: 'user-edit'
                , saveUrl: 'sys/user/save'
                , tableObj: tableObj
                , width: 500
                , height: 600
                , htmlData: data
            })
        }
        // 设置角色
        if (layEvent === 'settingRoles') {
            showDialog({
                title: '设置角色'
                , template: 'user-set'
                , saveUrl: 'sys/user/setuserrole'
                , tableObj: tableObj
                , width: 400
                , height: 500
                , htmlData: data
            })
        }
    });

    // 添加
    $("#user_add").click(function () {
        showDialog({
            title: '添加'
            , template: 'user-add'
            , saveUrl: 'sys/user/save'
            , tableObj: tableObj
            , width: 500
            , height: 600
            , beforeSubmit: function (d) {
                d.orgNo=nodeId;
                if($("#add_form input[name='userPwd']").val()!=$("#add_form input[name='reUserPwd']").val()){
                    layer.msg('两次密码不一致');
                    return false;
                }
                return true;
            }
        })
    });
    // 重置密码
    $("#user_reset").click(function () {
        var data = table.checkStatus('list_table').data;//已选中数据
        if (data.length == 0) {
            layer.msg("请至少选择一条");
            return;
        }
        var arr = [];
        for (var i = 0; i < data.length; i++) {
            arr.push(data[i].userId)
        }
        showConfirm('确认要重置密码吗？',function () {
            $.ajax({
                type: "GET",
                url: 'sys/user/resetpw',
                data: {"userId": arr.join(",")},
                success: function (result) {
                    if (result.success) {
                        layer.msg("重置成功");
                        reloadTable(tableObj);
                    }
                },
                error: function () {
                    layer.msg("处理异常");
                }
            })
        },0);
    });
    // 删除
    $("#user_del").click(function () {
        var data = table.checkStatus('list_table').data;//已选中数据
        if (data.length == 0) {
            layer.msg("请至少选择一条");
            return;
        }
        var arr = [];
        for (var i = 0; i < data.length; i++) {
            arr.push(data[i].userId);
        }
        showConfirm("确认删除？",function () {
            $.ajax({
                type: "GET",
                url: 'sys/user/delete',
                data: {"userId": arr.join(",")},
                success: function (result) {
                    if (result.success) {
                        layer.msg("删除成功");
                        reloadTable(tableObj);
                    }
                },
                error: function () {
                    layer.msg("删除异常");
                }
            })
        },0);
    });
    $("#query").click(function () {
        reloadTable(tableObj);
    });

    var ztreeObj = null;

    var setting = {
        data: {
            key: {
                title: "t"
            },
            simpleData: {
                enable: true
            }
        },
        view: {
            showIcon: true,
            showTitle: false
        },
        callback: {
            onClick: ztreeCallback
        }
    };


    //获取左侧ztree
    getZtree();

    function getZtree() {
        $.ajax({
            url: 'sys/org/list',
            success: function (result) {
                var zNodes = result;
                ztreeObj=initZtree("ztree", setting, zNodes);
            },
            error: function () {
                layer.msg("操作异常");
            }
        });
    }
    var nodeId;
    function ztreeCallback(event, treeId, treeNode) {
        nodeId= treeNode.id;
        $("#list_form input[name='orgNo']").val(nodeId);
        reloadTable(tableObj);
    }
});